package com.lzy.mall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lzy.mall.common.constant.WareConstant;
import com.lzy.mall.common.utils.PageParaUtils;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.ware.entity.WmsPurchaseDetail;
import com.lzy.mall.ware.dao.WmsPurchaseDetailDao;
import com.lzy.mall.ware.entity.WmsWareInfo;
import com.lzy.mall.ware.entity.WmsWareSku;
import com.lzy.mall.ware.service.WmsPurchaseDetailService;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * (WmsPurchaseDetail)表服务实现类
 *
 * @author lzy
 * @since 2021-08-24 19:01:45
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class WmsPurchaseDetailServiceImpl implements WmsPurchaseDetailService {
    private final WmsPurchaseDetailDao purchaseDetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);

        LambdaQuery<WmsPurchaseDetail> lambdaQuery = purchaseDetailDao.createLambdaQuery();

        String key = (String) params.get("key");
        if (StrUtil.isNotEmpty(key)){
            lambdaQuery.and(lambdaQuery.condition()
                    .andEq(WmsPurchaseDetail::getPurchaseId,key)
                    .orEq(WmsPurchaseDetail::getSkuId,key)
            );
        }
        String wareId = (String) params.get("wareId");
        if (StrUtil.isNotEmpty(wareId)){
            lambdaQuery.andEq(WmsPurchaseDetail::getWareId,wareId);
        }
        String status = (String) params.get("status");
        if (StrUtil.isNotEmpty(status)){
            lambdaQuery.andEq(WmsPurchaseDetail::getStatus,status);
        }
        PageQuery<WmsPurchaseDetail> page = lambdaQuery.page(pageQuery.getPageNumber(), pageQuery.getPageSize());
        return new PageUtils(page);
    }

    @Override
    public WmsPurchaseDetail getById(Long id) {
        WmsPurchaseDetail purchaseDetail = purchaseDetailDao.single(id);
        return purchaseDetail;
    }

    @Override
    public void save(WmsPurchaseDetail purchaseDetail) {
        purchaseDetailDao.insertTemplate(purchaseDetail);
    }

    @Override
    public void removeBatch(List<Long> ids) {
        purchaseDetailDao.createLambdaQuery()
                .andIn(WmsPurchaseDetail::getId,ids)
                .delete();
    }

    @Override
    public void update(WmsPurchaseDetail purchaseDetail) {
        purchaseDetailDao.updateTemplateById(purchaseDetail);
    }

    @Override
    public void updateBatchById(List<WmsPurchaseDetail> collect) {
        purchaseDetailDao.getSQLManager()
                .updateBatchTemplateById(WmsPurchaseDetail.class,collect);
    }

    @Override
    public void updateDetailByPurchaseIds(List<Long> purchaseIds) {
        List<WmsPurchaseDetail> collect = purchaseDetailDao.createLambdaQuery()
                .andIn(WmsPurchaseDetail::getPurchaseId, purchaseIds)
                .select()
                .stream()
                .map(item -> {
                    item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                    return item;
                }).collect(Collectors.toList());
        purchaseDetailDao.getSQLManager()
                .updateBatchTemplateById(WmsPurchaseDetail.class, collect);

    }
}
